<?php

/*
Copyright 2007 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Widget_Data.php');
require_once('BH_Widget.php');

class BH_Widget_Manager {
	var $errorMsg;
	private function add_widget($name, $class) {
		global $bhwd;
		
		if ($bhwd->find_widget($name) !== false) {
			$this->errorMsg = "'$name' alread exists";
			return false;
		}
		
		if (!$class) {
			$this->errorMsg="You must select a class";
			return false;
		}
		
		if (!class_exists($class)) {
			$this->errorMsg="class '$class' is unknown";
			return false;
		}
		
		if (!is_subclass_of($class, 'BH_Widget')) {
			$error="$class must be subclass of BH_Widget";
			return false;
		}
			
		$widget = $bhwd->create_widget($name, $class);

		$o = new $class($widget);
		
		register_sidebar_widget($name, array( $o, 'widget'));
		
		if ($o instanceof BH_Controllable_Widget) {
			$ctrl_dim = $bhwd->get_class($class);
			register_widget_control($name, array( $o, 'control'), $ctrl_dim->x, $ctrl_dim->y);
		}
		
		return true;
	}

	private function remove_widget($id) {
		global $bhwd;
		
		if (!($widget = $bhwd->get_widget($id))) {
			$this->errorMsg = "Don't know widget '$id'";
			return false;
		}
		
		$bhwd->set_widget_delete($id, true);
		
		unregister_sidebar_widget($widget->widget_name);
		unregister_widget_control($widget->widget_name);
		return true;
	}
	
	private function copy_widget($id, $target) {
		global $bhwd;
		
		if (!($w = $bhwd->get_widget($id))) {
			$this->errorMsg = "Don't know widget id '$id'";
			return false;
		}
		
		return $this->add_widget($target, $w->widget_class);
	}
	
	private function rename_widget($id, $name) {
		global $bhwd;
	
		if (!($widget = $bhwd->get_widget($id))) {
			$this->errorMsg = "Don't know widget '$id'";
			return false;
		}
		
		if ($bhwd->find_widget($name) !== false) {
			$this->errorMsg = "'$name' alread exists";
			return false;
		}
		
		$bhwd->set_widget_name($id, $name);
		
		return true;
	}
		
	function sidebar_admin_setup() {
		
		if (!isset($_POST['bh-widget-manager-submit']) ) 
			return;
	
		$submit = $_POST['bh-widget-manager-submit'];
		$class = $_POST['bh-widget-manager-class'];
		$new_name = $_POST['bh-widget-manager-name'];
		$name = $_POST['bh-widget-manager-object'];

		if ($submit == 'Add')
			$this->add_widget($new_name, $class);
		
		if ($submit == 'Copy')
			$this->copy_widget($name, $new_name);
		
		if ($submit == 'Delete')
			$this->remove_widget($name);
			
		if ($submit == 'Rename') 
			$this->rename_widget($name, $new_name);
	}

	function sidebar_admin_page() {
		global $bhwd;
		
		echo '<div class="wrap">'."\n";
		echo '<h2>BH Widgets</h2>'."\n";
		
		if ($this->errorMsg) {
			echo '<h3 style="color:red;">' . $this->errorMsg . '</h3>';
		}
		
		echo '<form method="POST">'. "\n";
		echo '<p style="line-height: 30px;">'. "\n";
		
		$objects = $bhwd->get_widgets();
		
		if (natcasesort($objects)) {
			echo '<select name="bh-widget-manager-object" >'. "\n";
			echo '<option value="">select widget</option>'."\n";

			foreach ( $bhwd->get_widgets() as $id => $widget)  {
				if (!$id || !$widget)
					continue;
					
				if ($widget->widget_delete)
					continue;
					
				echo '<option value="'. $id .'">'. $widget->widget_name .'</option>'. "\n";
			}
			
			echo '</select>'. "\n";
			echo '<span class="submit"><input type="submit" name="bh-widget-manager-submit" id="bh-widget-manager-submit" value="'. __('Delete') .'" /></span>'. "\n";
		}
		
		$classes = array_keys($bhwd->get_classes());
		

		if (natcasesort($classes)) {
			echo '<select name="bh-widget-manager-class" >'. "\n";
			echo '<option value="">select class</option>'."\n";
	
			foreach ( $classes as $class )
				echo '<option value="'. $class .'">'. $class .'</option>'. "\n";
	
			echo '</select>'. "\n";
	
			echo '<input type="text" name="bh-widget-manager-name"/>'. "\n";
			echo '<span class="submit"><input type="submit" name="bh-widget-manager-submit" id="bh-widget-manager-submit" value="'. __('Add') .'" /></span>'. "\n";

			if ($objects) {
				echo '<span class="submit"><input type="submit" name="bh-widget-manager-submit" id="bh-widget-manager-submit" value="'. __('Copy') .'" /></span>'. "\n";
				echo '<span class="submit"><input type="submit" name="bh-widget-manager-submit" id="bh-widget-manager-submit" value="'. __('Rename') .'" /></span>'. "\n";
			}
		}
		echo '</p>';
		echo '</form>'. "\n";
		echo '</div>'. "\n";
	}

	function widgets_init() {
		global $bhwd;
		
		if (!$bhwd) return;
		
		do_action('bh_widgets_init');
		
		if ($bhwd->get_widgets())
			foreach ($bhwd->get_widgets() as $id => $widget)  {
				
				if (!$id)
					continue;
					
				$class = $widget->widget_class;
				
				if (!class_exists($class))
					continue;
					
				$o = new $class($widget);
				
				if (!($o instanceof BH_Widget))
					continue;
				
				register_sidebar_widget($widget->widget_name, array( $o, 'widget'));
				
				if (!($o instanceof BH_Controllable_Widget))
					continue;
					
				if (!($ctrl_dim = $bhwd->get_class($class)))
					continue;				
					
				register_widget_control($widget->widget_name, array( $o, 'control'), $ctrl_dim['x'], $ctrl_dim['y']);
			}

		add_action('sidebar_admin_setup', array($this,'sidebar_admin_setup'));
		add_action('sidebar_admin_page', array($this,'sidebar_admin_page'));
	}
}

?>
